/**
 * Copyright (C) 2009 alex <me@alex.0x08.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



package uber.netbsd.community.spring.security;

import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import uber.netbsd.community.dao.Person;
import uber.netbsd.community.dao.PersonManager;

/**
 *
 * @author alex
 */
//@Service("userManager")
public class UserManagerImpl implements UserManager , UserDetailsService{

    @Override
    public Person getUser(String userId) {
       return PersonManager.getInstance().getPerson(Integer.valueOf(userId));
    }

    @Override
    public Person getUserByUsername(String username) throws UsernameNotFoundException {
        return PersonManager.getInstance().getPerson(username);
    }

    @Override
    public List getUsers() {
       return PersonManager.getInstance().getAllPeople();
    }

    @Override
    public Person saveUser(Person user) throws UserExistsException {
        PersonManager.getInstance().saveOrUpdate(user);
        return PersonManager.getInstance().getPerson(user.getUsername());
    }

    @Override
    public void removeUser(String userId) {
       PersonManager.getInstance().delete(Integer.valueOf(userId));
       
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
        return PersonManager.getInstance().getPerson(username);
    }

    @Override
    public String getUserPassword(String username) {
       Person p = PersonManager.getInstance().getPerson(username);
       if (p==null)
           return null;
       return p.getPassword();
    }

}
